package com.yunji.base.business;

import com.yunji.base.domain.AllianceDO;
import com.yunji.base.service.AllianceService;
import com.yunji.base.vo.AllianceDTO;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.user.business
 * @Title: AllianceBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-06-28 17:32:25      V1.0        HuaAo       新建类
 */
@Component
public class AllianceBusiness {

    @Resource
    AllianceService allianceService;

    /**
     * 获取加盟商列表
     *
     * @param alliance
     * @return
     */
    public List<AllianceDTO> selectSAllianceDTOList(AllianceDTO alliance) {
        List<AllianceDTO> list = allianceService.selectSAllianceDTOList(alliance);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

    /**
     * 获取加盟商详细信息
     *
     * @param id
     * @return
     */
    public AllianceDO selectSAllianceById(Long id) {
        return allianceService.selectSAllianceById(id);
    }

    /**
     * 新增加盟商
     *
     * @param sAlliance
     */
    public Long insertSAlliance(AllianceDO sAlliance) {
        Assert.isTrue(allianceService.insertSAlliance(sAlliance), "新增失败");
        return sAlliance.getId();
    }

    /**
     * 修改加盟商
     *
     * @param sAlliance
     */
    public void updateSAlliance(AllianceDO sAlliance) {
        Assert.isTrue(allianceService.updateSAlliance(sAlliance), "修改失败");
    }

    /**
     * 批量删除加盟商
     *
     * @param ids
     */
    public void deleteSAllianceByIds(Long[] ids) {
        Assert.isTrue(allianceService.deleteSAllianceByIds(ids), "删除失败");
    }

    /**
     * 获取当前用户还可申请加盟次数
     *
     * @param id
     * @return
     */
    public int selectSubmitNums(Long id) {
        return allianceService.selectSubmitNums(id);
    }

    /*-----------------------------------------搜索栏----------------------------------------*/

    /**
     * @param name 加盟名称
     * @Title searchName
     * @Desc 名称匹配搜索
     * @Date 2024-09-09 14:06:21.545
     * @Return List<String> 匹配结果
     */
    public List<String> searchName(String name) {
        Assert.isTrue((name.length() <= 6), "输入姓名不合规");
        AllianceDTO dto = new AllianceDTO();
        dto.setName(name);
        dto.setSearchFiled("name");
        List<String> list = allianceService.searchTableFiled(dto);
        return BeanUtils.isNotNull(list) ? list : Collections.emptyList();
    }

    /**
     * @param phone 电话号码
     * @Title searchPhone
     * @Desc 电话号码匹配搜索
     * @Date 2024-09-09 14:05:55.691
     * @Return List<String> 匹配结果
     */
    public List<String> searchPhone(String phone) {
        Assert.isTrue(phone.matches("^\\d{0,11}$"), "输入手机号不合规");
        AllianceDTO dto = new AllianceDTO();
        dto.setPhone(phone);
        dto.setSearchFiled("phone");
        List<String> list = allianceService.searchTableFiled(dto);
        return BeanUtils.isNotNull(list) ? list : Collections.emptyList();
    }
}